package tt.dz.service.imp.base;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tt.dz.dao.ext.base.OmsDeviceTypeDao;
import tt.dz.dao.ext.common.OmsFactoryListDao;
import tt.dz.dao.ext.system.OmsOperListDao;
import tt.dz.entity.base.JsonCommon;
import tt.dz.entity.base.JsonForList;
import tt.dz.entity.base.OmsCommonParam;
import tt.dz.entity.base.OmsEntityEnum;
import tt.dz.entity.base.OmsFactoryList;
import tt.dz.entity.exception.OmsDaoException;
import tt.dz.entity.exception.OmsServiceException;
import tt.dz.service.base.FactoryService;
import tt.dz.util.OmsConstant.MsgCodeEnum;
@Service @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class FactoryServiceImpl extends BaseService implements FactoryService
{
	@Autowired
	private OmsFactoryListDao omsFactoryListDaoImpl;
	@Autowired
	private OmsDeviceTypeDao omsDeviceTypeDaoImpl;
	@Autowired
	private OmsOperListDao operListDao;
	
	private Log log = LogFactory.getLog(FactoryServiceImpl.class);
	
	@Override
	public JsonForList<OmsFactoryList> findAllFactoryList() throws OmsServiceException 
	{
		OmsCommonParam param = new OmsCommonParam();
		try 
		{
			List<OmsFactoryList> list = omsFactoryListDaoImpl.pagingFindFactoryList(param,new OmsFactoryList());
			return this.buildSuccessJsonForList(param, list, 0);
		} 
		catch (OmsDaoException e) 
		{
			log.error(e.getMessage(),e);
			return this.buildError400JsonForList();
		}
	}

	@Override
	public JsonForList<OmsFactoryList> findPageFactoryList(OmsCommonParam param,OmsFactoryList obj) throws OmsServiceException {
		try 
		{
			int total = 0;
			List<OmsFactoryList> list = omsFactoryListDaoImpl.pagingFindFactoryList(param,obj);
			if (param.isQueryTotal())
			{
				total = omsFactoryListDaoImpl.findCountFactoryList(param);
			}
			return this.buildSuccessJsonForList(param, list, total);
		} 
		catch (OmsDaoException e) 
		{
			log.error(e.getMessage(),e);
			return this.buildError400JsonForList();
		}
	}

	@Override
	public synchronized  JsonCommon<OmsFactoryList> addFactoryList(OmsFactoryList factory)
	{
		try 
		{   
			//fac_login_name跟factory_id一致，factory_id是唯一的，不用校验，前端也不需要传递
//			//所有厂商账号不能重复
//			OmsFactoryList omsFactoryListTmp = new OmsFactoryList();
//			omsFactoryListTmp.setFac_login_name(factory.getFac_login_name());
//			OmsFactoryList omsFactoryListQury = omsFactoryListDaoImpl.findOneExample(omsFactoryListTmp, null, null);
//			if (omsFactoryListQury != null) {
//				JsonCommon  jfl = new JsonCommon();
//				jfl.setCode(MsgCodeEnum.ERROR400.getMsgCode());
//				jfl.setMsg(MsgCodeEnum.ERROR400.getMsgInfo()+":厂商账号不能重复");
//				return jfl;
//			}
			
			String maxId = omsFactoryListDaoImpl.findMaxIdFactryList();
			String maxNumStr  = "0";
			if (StringUtils.isNotEmpty(maxId))
			{
				maxNumStr = maxId.replaceFirst("^0*", "");
			}
			//利用正则表达式将前缀0去掉
			if (StringUtils.isNumeric(maxNumStr))
			{
				int currentId = Integer.parseInt(maxNumStr)+1;
				String currentIdStr = String.format("%08d", currentId);
				factory.setFactory_id(currentIdStr);
				factory.setFac_login_name(currentIdStr);
				omsFactoryListDaoImpl.addFactoryList(factory);
				return this.buildSuccessJsonCommon(factory);
			}
			else
			{
				return this.buildOtherJsonCommon(MsgCodeEnum.ERROR400.getMsgCode(), "MaxId is Not Numeric");
			}
		} 
		catch (OmsDaoException e) 
		{
			log.error(e.getMessage(),e);
			return this.buildError400JsonCommon();
		}
	}
	

	@Override
	public JsonCommon<OmsFactoryList> updateFactoryList(OmsFactoryList factory) 
	{
		try 
		{
			if(StringUtils.isEmpty(factory.getFactory_id()))
			{
				return this.buildOtherJsonCommon(MsgCodeEnum.ERROR400.getMsgCode(), "error:the request parameter factory_id is empty");
			}
//			//所有厂商账号不能重复
//			OmsFactoryList omsFactoryListTmp = new OmsFactoryList();
//		    String condition = " where factory_id <>" + factory.getFactory_id() + " and fac_login_name = '" + factory.getFac_login_name() + "'";
//		    List<OmsFactoryList> listOmsFactoryList = omsFactoryListDaoImpl.findListByCondition(omsFactoryListTmp, condition);
//			if (listOmsFactoryList.size() >0 ) {
//				JsonCommon  jfl = new JsonCommon();
//				jfl.setCode(MsgCodeEnum.ERROR400.getMsgCode());
//				jfl.setMsg(MsgCodeEnum.ERROR400.getMsgInfo()+":厂商账号不能重复");
//				return jfl;
//			}
			
			omsFactoryListDaoImpl.updateFactoryList(factory);
			return this.buildSuccessJsonCommon(factory);
		} 
		catch (OmsDaoException e) 
		{
			log.error(e.getMessage(),e);
			return this.buildError400JsonCommon();
		}
	}

	@Override
	public JsonCommon<?> deleteFactoryById(String fid)
	{
		try 
		{
			if(StringUtils.isEmpty(fid))
			{
				return this.buildOtherJsonCommon(MsgCodeEnum.ERROR400.getMsgCode(), "error:the request parameter factory_id is empty");
			}
			omsFactoryListDaoImpl.deleteFactoryList(fid);
			//逻辑删除厂商关联设备
			omsDeviceTypeDaoImpl.updateByRId(fid);
			//级联删除厂商关联的用户
			operListDao.logicDelOperByEntity(OmsEntityEnum.FACTORY.getValue(), fid);
			
			return this.buildSuccessJsonCommon("success");
		} 
		catch (OmsDaoException e) 
		{
			log.error(e.getMessage(),e);
			return this.buildError400JsonCommon();
		}
	}


}
